#include "precompile.h"

#include <windows.h>
#include "sys/io/Path.h"
using namespace io;

const wchar_t Path::PathSeperator = L'\\';
const wchar_t* Path::PathSeperatorString = L"\\";

String Path::CombinePath(const String& path1,const String& path2)
{
	wchar_t result[_MAX_PATH];
	::wcscpy_s(result, _MAX_PATH,path1.c_str());
	uint16 wCurrent = result[0];
	if (wCurrent == 0)
	{
		::wcscpy_s(result, _MAX_PATH, path2.c_str());
		return result;
	}

	int32 nIndex = 0;
	uint16 wPrev;
	do
	{
		wPrev = wCurrent;
		nIndex++;
		wCurrent = result[nIndex];
	} while (wCurrent != 0);

	if ((wPrev != PathSeperator) && (wPrev != L':'))
		::wcscat_s(result,_MAX_PATH, PathSeperatorString);

	::wcscat_s(result,_MAX_PATH, path2.c_str());
	return result;
}

String Path::CombineExtension(const String& path,const String& extension)
{
	wchar_t result[_MAX_PATH];
	::wcscpy_s(result, _MAX_PATH, path.c_str());
	::wcscat_s(result,_MAX_PATH, L".");
	::wcscat_s(result,_MAX_PATH, extension.c_str());

	return result;
}

String Path::ReplaceExtension(const String& path,const String& extension)
{
	wchar_t result[_MAX_PATH];
	wchar_t strTempDrive[_MAX_DRIVE];
	wchar_t strTempDir[_MAX_DIR];
	wchar_t strTempFileName[_MAX_FNAME];
	wchar_t strTempExt[_MAX_EXT];

	::_wsplitpath_s(path.c_str(), strTempDrive,_MAX_DRIVE, strTempDir,_MAX_DIR, strTempFileName,_MAX_FNAME, strTempExt,_MAX_EXT);
	::wcscpy_s(strTempExt,_MAX_EXT, L".");
	::wcscpy_s(strTempExt,_MAX_EXT, extension.c_str());

	::_wmakepath_s(result,_MAX_PATH, strTempDrive, strTempDir, strTempFileName, strTempExt);

	return result;
}

bool Path::IsFullPath(const String& filename)
{
	wchar_t drive[_MAX_DRIVE];

	_wsplitpath_s(filename.c_str(), drive ,_MAX_DRIVE, NULL,0, NULL,0, NULL,0);
	String vstrDrive;
	vstrDrive=drive;
	if (vstrDrive.size()>0)
		return true;

	if (filename.size()>2)
	{
		if (filename[0]==PathSeperator&&filename[1]==PathSeperator)
			return true;
	}
	return false;
}

bool Path::IsRemotePath(const String& filename) 
{
	String temp = filename;

	String::size_type pos = temp.rfind(PathSeperator);
	if( String::npos == pos ) 
		return false;
	temp.resize(pos+1);
	return  DRIVE_REMOTE == ::GetDriveTypeW( temp.c_str() );
}

String Path::GetTitle(const String& filename) 
{
	String titleWithExtension = GetTitleWithExtension(filename);
	int32 index = static_cast<int32>(titleWithExtension.rfind(L'.'));
	if (index < 0)
		return titleWithExtension;
	return titleWithExtension.substr(0, index);
}

String Path::GetTitleWithExtension(const String& filename) 
{
	int32 index = static_cast<int32>(filename.rfind(PathSeperator));
	if (index < 0)
		return filename;
	return filename.substr(index+1);
}

String Path::GetExtension(const String& filename)
{
	int32 index = static_cast<int32>(filename.rfind(L'.'));
	if (index < 0)
		return filename;
	return filename.substr(index+1);
};

String Path::GetDirectoryName(const String& filename)
{
	int32 index=static_cast<int32>(filename.rfind(PathSeperator));
	if (index<0)
		return filename;
	return filename.substr(0, index+1);
}

String Path::GetExeDirectory()
{
	String fullpath;
	fullpath.resize(MAX_PATH*2);
	fullpath.resize(::GetModuleFileNameW(NULL,const_cast<wchar_t*>(fullpath.data()),(uint32)fullpath.size()));
	String::size_type pos=fullpath.rfind(PathSeperator);
	if(pos==String::npos)
		return L"";

	return fullpath.substr(0,pos);
}